package org.black.phoneUtility.sms.reply.app.fragment;

import org.black.phoneUtility.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;

public class SmsReplyDelayTimeForSmsDialogFragment extends DialogFragment {
	public static final String TAG = SmsReplyDelayTimeForSmsDialogFragment.class
			.getName();
	public static final String ARGUMENTS_DELAY_TIME = "arguments_delay_time";

	public interface SmsReplyDelayTimeForSmsDialogListener {
		public void onSmsReplyDelayTimeDialogForSmsPositiveClick(long delayTime);

		public void onSmsReplyDelayTimeDialogForSmsNegativeClick();
	}

	private SmsReplyDelayTimeForSmsDialogListener smsReplyDelayTimeForSmsDialogListener;

	public static final long THIRTY_SECONDS_IN_MILLISECONDS = 30l * 1000l;
	public static final long ONE_MINUTE_IN_MILLISECONDS = 1l * 60l * 1000l;
	public static final long FIVE_MINUTES_IN_MILLISECONDS = 5l * ONE_MINUTE_IN_MILLISECONDS;
	public static final long TEN_MINUTES_IN_MILLISECONDS = 10l * ONE_MINUTE_IN_MILLISECONDS;

	private long delayTime = THIRTY_SECONDS_IN_MILLISECONDS;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		Bundle bundle = getArguments();
		delayTime = bundle.getLong(ARGUMENTS_DELAY_TIME);
		int checkPosition = 0;
		if (delayTime == ONE_MINUTE_IN_MILLISECONDS) {
			checkPosition = 1;
		} else if (delayTime == FIVE_MINUTES_IN_MILLISECONDS) {
			checkPosition = 2;
		} else if (delayTime == TEN_MINUTES_IN_MILLISECONDS) {
			checkPosition = 3;
		}
		builder.setTitle(
				R.string.sms_reply_add_edit_delay_reply_sms_delay_time_title)
				.setSingleChoiceItems(
						R.array.sms_reply_add_edit_delay_reply_sms_delay_time_array,
						checkPosition, new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								switch (which) {
								case 1:
									delayTime = ONE_MINUTE_IN_MILLISECONDS;
									break;
								case 2:
									delayTime = FIVE_MINUTES_IN_MILLISECONDS;
									break;
								case 3:
									delayTime = TEN_MINUTES_IN_MILLISECONDS;
									break;
								default:
									delayTime = THIRTY_SECONDS_IN_MILLISECONDS;
									break;
								}
							}
						})
				.setPositiveButton(android.R.string.ok, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						smsReplyDelayTimeForSmsDialogListener
								.onSmsReplyDelayTimeDialogForSmsPositiveClick(delayTime);
					}
				})
				.setNegativeButton(android.R.string.cancel,
						new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								smsReplyDelayTimeForSmsDialogListener
										.onSmsReplyDelayTimeDialogForSmsNegativeClick();
							}
						});

		return builder.create();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsReplyDelayTimeForSmsDialogListener = (SmsReplyDelayTimeForSmsDialogListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement SmsReplyDelayTimeForSmsDialogListener");
		}
	}
}
